<?php

namespace app\user\controller\mall\oper;

use app\common\controller\Userend;

/**
 * 站内通知
 *
 * @icon fa fa-circle-o
 */
class Notification extends Userend
{
    
    /**
     * Notification模型对象
     * @var \app\common\model\mall\oper\Notification
     */
    protected $model = null;
    protected $noNeedRight = ['selectpage'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\mall\oper\Notification;
        $id = $this->request->param('id', $this->auth->id);
        $user = \addons\mall\model\User::get($id);
        if (!$user) {
            $this->error("未找到指定用户");
        }
        $user->combine();

        $this->user = $user;

        $this->view->assign('__user__', $user);

    }

    /**
     * 我的通知
     */
    public function index()
    {
        if ($this->auth->id != $this->user->id) {
            $this->error("无法进行越权操作");
        }
        $act = $this->request->request('act');
        if ($act) {
            if ($act == 'marktopall') {
                if ($this->user->notifications || $this->user->messages) {
                    \addons\mall\model\User::where('user_id', $this->user->id)->update(['notifications' => 0, 'messages' => 0]);
                    $this->auth->getUser()->mall->notifications = 0;
                    $this->auth->getUser()->mall->messages = 0;
                }
            } else if ($act == 'marktop') {
                if ($this->user->notifications) {
                    \addons\mall\model\User::where('user_id', $this->user->id)->update(['notifications' => 0]);
                    $this->auth->getUser()->mall->notifications = 0;
                }
            } else if ($act == 'markall') {
                $this->model->where('to_user_id', $this->auth->id)->where('isread', 0)->update(['isread' => 1]);
                $this->user->data(['notifications' => 0])->save();
                $this->success('标记成功');
            } else {
                $notification_id = $this->request->request('notification_id');
                $notification = $this->model->where('id', $notification_id)->where('to_user_id', $this->auth->id)->find();
                if (!$notification) {
                    $this->error('未找到指定信息');
                }
                if ($act == 'del') {
                    $notification->delete();
                    $this->user->setDec('notifications');
                    $this->success('删除成功');
                } else if ($act == 'mark') {
                    $notification->isread = 1;
                    $notification->save();
                    $this->user->setDec('notifications');
                    $this->success('标记成功');
                }
            }
        }
        $notificationList = $this->model->with('from')
            ->where('to_user_id', $this->user->id)
            ->order('id desc')
            ->paginate();
        $this->view->assign('title', '我的通知');
        $this->view->assign('notificationList', $notificationList);

        if ($this->request->isAjax()) {
            return $this->view->fetch("mall/oper/ajax/get_notification_list");
        }
        return $this->view->fetch();
    }
}
